package com.qyl.http_upload.client;

import com.alibaba.fastjson.JSONObject;
import com.qyl.socket_upload.server.FileServer;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class FileClient {

    // contentType
//    private static String final




        public void get() {
            try {
                StringBuffer get = new StringBuffer(200);
                get.append("GET /file/definition?name=importsource HTTP/1.1\r\n");
                get.append("Host: 127.0.0.1:8080\r\n");
                get.append("Accept: text/html\r\n");
                get.append("Connection: Close\r\n");
                get.append("\r\n");
                get.append("");

                System.out.println(get);

                // socket客户端连接本机的tomcat服务器，端口为8080
                Socket socket = new Socket("127.0.0.1", 8080);
                socket.setSoTimeout(30000);

                // socket客户端发送登录请求
                PrintWriter outWriter = new PrintWriter(socket.getOutputStream());
                // 发送post请求
                outWriter.println(get);
                outWriter.flush();

                // socket客户端接收tomcat返回的数据
                BufferedReader inReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 以下是服务器返回的数据
                System.out.println("**************** Server responsed ****************");
                String tmp = "";
                while ((tmp = inReader.readLine()) != null) {
                    // 解析服务器返回的数据，做相应的处理
                    System.out.println(tmp);
                }
                outWriter.close();
                inReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    /**
     * 发送文件
     */
    public void sendFile(String path) throws Exception {
//        File file = new File(path);
        //创建连接，创建文件输入流，网络输出流
        Socket socket = new Socket("127.0.0.1", 8080);
//        socket.setKeepAlive(true);
//        socket.setTcpNoDelay(true);
//        socket.setSoTimeout(100000000);
//        //        InputStream in = new FileInputStream(path);
////        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        OutputStream out = socket.getOutputStream();
        byte[] bytes = sendData().getBytes();
        out.write(bytes);
//        out.flush();
//        InputStream in = socket.getInputStream();
//        BufferedReader br = new BufferedReader(new InputStreamReader(in));
//        String line = "";
//        while ((line  = br.readLine()) != null){
//            System.out.println(line);
//        }
//        System.out.println("客户端发送完毕！");


//        System.out.println(get);

        // socket客户端连接本机的tomcat服务器，端口为8080
//        Socket socket = new Socket("127.0.0.1", 8080);
//        socket.setSoTimeout(30000);
//
//        // socket客户端发送登录请求
//        PrintWriter outWriter = new PrintWriter(socket.getOutputStream());
//        // 发送post请求
//        outWriter.println(sendData()+"\r\n");
//        outWriter.flush();
//
//        // socket客户端接收tomcat返回的数据
//        BufferedReader inReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        // 以下是服务器返回的数据
//        System.out.println("**************** Server responsed ****************");
//        String tmp = "";
//        while ((tmp = inReader.readLine()) != null) {
//            // 解析服务器返回的数据，做相应的处理
//            System.out.println(tmp);
//        }
//        outWriter.close();
//        inReader.close();
    }

    // 发送报文
    private String sendData(){
        return """
POST /file/text HTTP/1.1
Host: 127.0.0.1
Content-Type: multipart/form-data; boundary=--------------------------977408596718098001400365
Content-Length: 1

----------------------------977408596718098001400365
Content-Disposition: form-data; name="id"

111
----------------------------977408596718098001400365
Content-Disposition: form-data; name="name"

222
----------------------------977408596718098001400365
Content-Disposition: form-data; name="file"; filename="text.txt"
Content-Type: text/plain

qwioerjweiorhf
.........
----------------------------977408596718098001400365--
""";
    }

    public static void main(String[] args) throws Exception {
        new FileClient().sendFile("D:\\text.txt");
//        new FileClient().get();
//        System.out.println(sendData());
    }
}
